package com.demo.spring.actions;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.demo.spring.bussness.Bdo;
import com.demo.spring.forms.TestForm;
import com.demo.spring.ibussness.ITest;
@Controller
public class TestAction extends SimpleFormController {
	
	@Autowired
	private Bdo bdo;

	@Autowired 
	@Qualifier("test1") 
	private ITest test; 

	@RequestMapping("/test")
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		bdo.out();
		test.sayHell();
		TestForm testForm = (TestForm) command;
		if (testForm.getStr1().equals("admin")
				&& testForm.getStr2().equals("admin")) {
			request.getSession().setAttribute("str1", testForm.getStr1());
			request.getSession().setAttribute("str2", testForm.getStr2());
			return new ModelAndView(getSuccessView());
		} else {
			Map modle = errors.getModel();
			modle.put("testForm", testForm);
			return new ModelAndView(getFormView(), modle);
		}
	}
}
